//this file is used to provide the basic classes of 
//the bookLib project
//
//classes:BookManager,UserManager


/*class BookManager
 *
 *usage:to handle the checkout or search request
 *
 *
 *
 *
 *since version: 0.4
 *
 */
//instance bookManager
var bookManager = null;

//global function
function getURL(URL){
    var xhr = new XMLHttpRequest();
    xhr.open("GET",URL,false);
    xhr.send();
    if(xhr.status==200)
	return xhr.responseText;
    else
    {
	return "FAIL";
    }	
}
//classes
function BookManager()
{
    this.bookInfo = null;
}
BookManager.prototype.getBookInfo = function(ID)
{
    var result = getURL("api/get_book_info.php?ID="+ID);
    if(result!="FAIL")
	var bookJson = JSON.parse(result);
    else
    {
	return null;
    }
    this.bookInfo = bookJson;
    return bookJson;
}
BookManager.prototype.searchBooks = function(keyword,from,to)
{
    var jsonBooks = getURL("api/search_books.php?"
			   +"keyword="+keyword
			   +"&from="+from
			   +"&to="+to);
    if(jsonBooks!="FAIL"&&jsonBooks!="")
    {
	return JSON.parse(jsonBooks);
    }
    return null;
}
BookManager.prototype.searchBooksNum = function(keyword)
{
    return getURL("api/search_books_num.php?keyword="+keyword);
}
BookManager.prototype.checkoutBook = function(ID)
{
    if(getURL("api/checkout_book.php?ID="+ID)=="OK")
	return true;
    else
	return false;
}
BookManager.prototype.returnBook = function(ID)
{
    if(getURL("api/return_book.php?ID="+ID)=="OK")
	return true;
    else
	return false;
}
BookManager.prototype.addBook = function(ID,name,location,publisher)
{
    if(getURL("api/add_book.php?ID="+ID
	      +"&name="+name
	      +"&location="+location
	      +"&publisher="+publisher)=="OK")
    {
	return true;
    }
    else
    {
	return false;
    }

}
BookManager.prototype.delBook = function(ID)
{
    if(getURL("api/del_book.php?ID="+ID)=="OK")
	return true;
    else
	return false;
}
/*class UserManager
 *
 *usage: to handle the userRequest like regist or login
 *
 *
 *
 *since version: 0.5
 *
 */
//instance
var userManager = null;
function UserManager()
{
    this.isSyn = false;
    this.clientInfo = null;
    this.checkoutHistory = null;
    this.currentCheckout = null;
    this.isLogin = false;
    this.username = null;;
    this.checkLoginStatus();
} 
//check and return the loginStatus
//and modiry the this.isLogin attr
UserManager.prototype.checkLoginStatus = function()
{
    if(this.isSyn)
    {
	return this.isLogin;
    }
    this.isSyn = true;
    if(getURL("api/is_login.php")=="YES")
    {
	this.isLogin = true;
	return true;
    }
    else
    {
	this.isLogin = false;
	return false;
    }
}
UserManager.prototype.syn = function()
{
    this.syn = true;
    if(this.checkLoginStatus())
    {
	this.username = document.cookie
    }
}
UserManager.prototype.login = function(username,password)
{
    var result = getURL("api/login.php?"
			+"username="+username
			+"&password="+password); 
    if(result =="OK")
    {
	this.isLogin = true;
	this.isSyn = true;
	this.username = username;
	return true;
    }
    else
    {
	this.isLogin = false;
	this.isSyn = true;
	return false;
    }
}
UserManager.prototype.logout = function()
{
    var result = getURL("api/logout.php");
    if(result =="OK")
    {
	UserManager.call(this);
	return true;
    }
    else
    {
	return false;
    }
}
UserManager.prototype.getClientInfo = function(username)
{
    var clientInfoJson;
    var result;
    if(username)
    {
	result = getURL("api/get_client_info.php?username="+username);
	if(result!="FAIL")
	    clientInfoJson =JSON.parse(result); 
	else
	{
	    return false;
	}
	if(username == this.username)
	    this.clientInfo = clientInfoJson;
    }
    else
    {
	if(this.clientInfo!=null)
	    return this.clientInfo;
	result = getURL("api/get_client_info.php");
	if(result!="FAIL")
	    clientInfoJson = JSON.parse(result);
	else
	{
	    return false;
	}
	this.clientInfo = clientInfoJson;
    }
    return clientInfoJson;
}
UserManager.prototype.addClient = function(username,password,mailbox,realname)
{
    if(!username||!password||!mailbox||!realname)
	return false;
    var result = getURL("api/add_client.php?"
			+"username="+username
			+"&password="+password
			+"&mailbox="+mailbox
			+"&realname="+realname);

    if(result=="OK")
    {
	return true;
    }
    else
    {
	return false;
    }
}
UserManager.prototype.getCheckoutHistory = function()
{
    var result =  getURL("api/get_client_checkout_history");
    if(!result||result=="")
    {
	this.checkoutHistory = null;
	return null;
    }
    else
    {
	this.checkoutHistory = JSON.parse(result);
	return this.checkoutHistory;
    }
}
UserManager.prototype.getCurrentCheckout = function()
{
    var result = getURL("api/get_client_current_checkout.php");
    if(!result||result=="")
    {
	this.currentCheckout = null;
	return null
    }
    else
    {
	this.currentCheckout = JSON.parse(result);
	return this.currentCheckout;
    }
}